<template>
  <a-modal
    title="编辑活动"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >


    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <!--companyId-->
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="所属公司">
          <a-select
            v-decorator="[ 'companyId', {rules: [{ required: true, message: '请输入商户名称' }],initialValue: defaultCompany}]"
            placeholder="请选择所属公司">
            <a-select-option :value="company.id" v-for="company in companyList" :key="company.id">
              {{company.name}}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="活动标题">
          <a-input placeholder="活动标题" v-decorator="[ 'title', {rules: [{ required: true, message: '请输入类别名称' }]}]"
          ></a-input>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="活动简介">
          <a-input placeholder="活动简介" v-decorator="[ 'introduction', {rules: [{ required: true, message: '请输入类别名称' }]}]"
          ></a-input>
        </a-form-item>


        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="活动详情">
          <quill-editor
            v-model="content"
            ref="myQuillEditor"
            :options="editorOption"
            @blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
            @change="onEditorChange($event)">
          </quill-editor>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { save,select } from '@api/message/activity'
  import { selectAllCompany } from '@api/system/company'
  import { quillEditor } from 'vue-quill-editor'
  import pick from 'lodash.pick'

  import 'quill/dist/quill.core.css'
  import 'quill/dist/quill.snow.css'
  import 'quill/dist/quill.bubble.css'
  export default {
    components: {
      quillEditor
    },
    //--############  data() ###############-->
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        mdl: {},
        form: this.$form.createForm(this),

        isDisabled: false,

        companyList: [],
        defaultCompany:'',

        editorOption: {
          modules: {
            toolbar: [
              ['bold', 'italic', 'underline', 'strike', 'clean'],
              ['blockquote', 'code-block'],
              [{ 'list': 'ordered' }, { 'list': 'bullet' }],
              [{ 'script': 'sub' }, { 'script': 'super' }],
              [{ 'indent': '-1' }, { 'indent': '+1' }],
              [{ 'direction': 'rtl' }],
              [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
              [{ 'color': [] }, { 'background': [] }],
              ['image']
            ]
          }
        },
        content: ''
      }
    },
    //--############  data() ###############-->

    created() {
      this.getAllCompany()
    },

    //--############  methods() ###############-->
    methods: {
      getAllCompany: function() {
        selectAllCompany().then(res => {
          if (res.code === 200) {
            this.companyList = res.result;
          } else {
            this.$notification.error({
              message: '错误',
              description: res.message,
              duration: 4
            })
          }
        })
      },
      edit(record) {
        this.isDisabled = true
        this.mdl = Object.assign({}, record)
        this.visible = true

        select(this.mdl.id).then(res => {
          this.mdl = res.result
          console.log( this.mdl)
          this.content = res.result.content
          this.form.setFieldsValue(pick(this.mdl, 'companyId', 'title', 'introduction'))
        })
      },
      onEditorBlur() {
      }, // 失去焦点事件
      onEditorFocus() {
      }, // 获得焦点事件
      onEditorChange() {
      }, // 内容改变事件
      // 转码
      escapeStringHTML(str) {
        str = str.replace(/&lt;/g, '<')
        str = str.replace(/&gt;/g, '>')
        return str
      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.content = ''
        this.mdl = {}
      },
      handleOk() {
        const _this = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          // 验证表单没错误
          if (!err) {
            values.id = this.mdl.id
            values.content = this.content
            _this.confirmLoading = true
            save(values).then((res) => {
              if (res.code === 200) {
                _this.$message.success('保存成功')
                _this.$emit('ok')
                _this.close()
              } else {
                this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
            }).catch(() => {
              // Do something
            }).finally(() => {
              _this.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      }
    }
    //--############  methods() ###############-->
  }
</script>

<style>
  .ql-container {
    height: 300px;
  }
</style>
